<template>
  <div>
    <crumbs :BreadCrumb="BreadCrumb"></crumbs>
    <div class="top_b">
      <!-- <div class="top_inp"> -->
      <el-form :inline="true" :model="searchForm" ref="ruleFormed">
        <div>
          <el-form-item label="体检单号" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-input class="input_c" v-model="searchForm.orderNo" clearable></el-input>
          </el-form-item>
          <el-form-item label="单位名称" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-input class="input_c" v-model="searchForm.enterpriseName" clearable></el-input>
          </el-form-item>
          <el-form-item label="合同名称" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-input class="input_c" v-model="searchForm.contractName" clearable></el-input>
          </el-form-item>
          <el-form-item label="二级部门" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-input class="input_c" v-model="searchForm.twoLevelDeptName" clearable></el-input>
          </el-form-item>
          <el-form-item label="客户类型" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-select v-model="searchForm.customerType" clearable>
              <el-option label="个人" :value="1"></el-option>
              <el-option label="团体" :value="2"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="打印状态" style="white-space: nowrap;" :label-width="formLabelWidth" >
            <el-select v-model="searchForm.status" clearable>
              <el-option label="未打印" :value="1"></el-option>
              <el-option label="已打印" :value="2"></el-option>
            </el-select>
          </el-form-item>
           <el-form-item label="姓名" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-input class="input_c" v-model="searchForm.customerName" clearable></el-input>
          </el-form-item>
          <el-form-item label="体检日期" style="white-space: nowrap;" :label-width="formLabelWidth">
            <el-date-picker
              v-model="checkDate"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              clearable
            >
              </el-date-picker>
          </el-form-item>
          <el-form-item label="" :label-width="formLabelWidth" class="search">
              <el-button type="primary" @click="getList">查询</el-button>
            </el-form-item>
        </div>
      </el-form>
    </div>
    <!-- 列表 -->
    <div>
      <el-table
          ref="multipleTable"
          :data="list"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>
        <el-table-column
            prop="orderNo"
            label="体检单号"
            width="150">
        </el-table-column>
        <el-table-column
            prop="customerName"
            label="姓名"
            width="150">
        </el-table-column>
        <el-table-column
            prop="sex"
            label="性别"
            width="80">
          <template slot-scope="scope">{{ scope.row.sex|formatSex }}</template>
        </el-table-column>
        <el-table-column
            prop="age"
            label="年龄"
            width="80">
        </el-table-column>
        <el-table-column
            prop="telephone"
            label="联系方式"
            width="150">
        </el-table-column>
        <el-table-column
            prop="enterpriseName"
            label="单位名称"
            width="150">
        </el-table-column>
        <el-table-column
            prop="contractName"
            label="团检合同"
            width="150">
        </el-table-column>
        <el-table-column
            prop="signDatetime"
            label="登记时间"
            width="150">
          <template slot-scope="scope">{{ scope.row.signDatetime|formatDate }}</template>
        </el-table-column>
        <el-table-column prop="reviewDoctorName" label="复检医生" width="150"></el-table-column>
        <el-table-column prop="reviewTime" label="复检日期" width="150">
          <template slot-scope="scope">{{ scope.row.reviewTime|formatDate }}</template>
        </el-table-column>
        <el-table-column prop="status" label="报告状态" width="150">
          <template slot-scope="scope">{{ scope.row.printStatus === 1 ? '未打印':'已打印' }}</template>
        </el-table-column>
        <el-table-column prop="printCount" label="打印次数" width="150"></el-table-column>
        <el-table-column prop="reportPrintUser" label="打印人" width="150"></el-table-column>
        <el-table-column width="220" prop="address" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button @click="viewRes(scope.row)" type="primary" size="small">预览</el-button>
            <el-button @click="printRes(scope.row)" type="primary" size="small">打印</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageNo"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
      </el-pagination>
    </div>
    <!-- 弹框 -->
  </div>
</template>

<script>

import crumbs from "../../views/Crumbs.vue";
import { formatTimeToStr } from "../../common/utils/date.js";
export default {

  components: {
    crumbs,
  },
  data() {
    return {
      dict:{},
      searchForm:{},
      checkDate:[
        formatTimeToStr(new Date(new Date().setMonth(new Date().getMonth() - 1)), "yyyy-MM-dd"),
        formatTimeToStr(new Date(), "yyyy-MM-dd")
      ],
      total:0,
      pageSize: 10,
      pageNo: 1,

      formLabelWidth: '80px',
      BreadCrumb: [
        {path: "", name: "检后管理"},
        {path: "", name: "报告打印"},
      ],

      list: [
        {Name: 'sadjkasd'}
      ],

    }
  },
  filters:{
    formatSex: function (val) {
      var dict = {1: "男", 2: "女"}
      return dict[val]
    },
    formatType: function (val) {
      var dict = {1: "普通体检", 2: "单位体检", 3: "入职体检", 4: "职业体检", 5: "婚育体检", 6: "老年专项体检", 7: "健康体检等"}
      return dict[val]
    },
    formatStatus: function (val) {
      var dict = {1: "未登记", 2: "登记", 4: "检中", 8: "交表", 16: "初检", 32: "复检", 64: "打印"}
      return dict[val]
    },
    formatDate: function (time) {
      if (time != null && time !== "") {
        var date = new Date(time);
        return formatTimeToStr(date, "yyyy-MM-dd hh:mm");
      } else {
        return "";
      }
    },
  },
  created() {
    this.searchDictList()
    this.getList()
  },
  methods: {
    searchDictList() {
      this.$axios.searchDictList({
        codes: ["tyType", "xmType", "jgType", "mzType", "gwType", "zwType", "ybType", "dwxzType", 'zylbType', 'zjType'],
      }).then((res) => {
        if (res.code === "00000") {
          res.data.forEach(v => {
            this.dict[v.dictCode] = v.items
          })
        }
      });
    },
    getList(){
      if(this.checkDate && this.checkDate.length===2){
        this.searchForm.startSignDatetime = formatTimeToStr(this.checkDate[0], "yyyy-MM-dd")
        this.searchForm.endSignDatetime = formatTimeToStr(this.checkDate[1], "yyyy-MM-dd")
      }else {
        delete this.searchForm.startSignDatetime
        delete this.searchForm.endSignDatetime
      }
      this.doGetList(this.searchForm)
    },
    doGetList(req){
      this.$axios.getPeExamReportsList(req).
      then(res => {
        if (res.code === "00000") {
          this.list = res.data.records
          this.total = res.data.total
        }
      })
    },
    // 每页几条
    handleSizeChange(val) {
      this.pageSize = val
      const req = {
      	...this.searchForm,
				pageSize:this.pageSize,
				pageNo:this.pageNo,
      }
      this.doGetList(req)
    },
    // 第几页
    handleCurrentChange(val) {
      this.pageNo = val
      const req = {
      	...this.searchForm,
				pageSize:this.pageSize,
				pageNo:this.pageNo,
      }
      this.doGetList(req)
    },

		handleSelectionChange(val) {
			this.multipleSelection = val;
		},
    viewRes(row){
      window.open(row.reportPrintUrl, "_blank");
    },
    printRes(row) {
      const index = window.localStorage.getItem('zydIndex')
      if (index > -1 && index !== undefined && index !== null) {
        const lodop = window.LODOP
        lodop.SET_LICENSES("","83A700E0C70546DE025B3B2260A1395DB69","","")
        lodop.PRINT_INIT()
        lodop.PRINT_INITA('0mm', '0mm', '210mm', '297mm', '')
        lodop.SET_PRINTER_INDEXA('orderPrint')
        lodop.SET_PRINT_PAGESIZE(0, '0mm', '0mm', '210mm', '297mm', '报告')
        lodop.ADD_PRINT_PDF('2%', '2%', '96%', '96%', this.downloadPDF(row.reportPrintUrl))
        // lodop.PREVIEW()
        lodop.PRINT()
      } else {
        this.$message.error('请先修改打印设置中报告所使用的打印机(同指引单)')
      }
    },
    downloadPDF(url){
      if (!(/^https?:/i.test(url))) return;
      if (window.XMLHttpRequest) var xhr = new XMLHttpRequest(); else var xhr = new ActiveXObject("MSXML2.XMLHTTP");
      xhr.open('GET',url,false);
      if (xhr.overrideMimeType)
      try{
        xhr.responseType = 'arraybuffer';
        var arrybuffer=true;
      }catch(err){
        xhr.overrideMimeType('text/plain; charset=x-user-defined');
      }
      xhr.send(null);
      var data = xhr.response || xhr.responseBody;
      if (typeof Uint8Array !== 'undefined') {
        if (arrybuffer) var dataArray = new Uint8Array(data); else {
          var dataArray = new Uint8Array(data.length);
          for (var i = 0; i < dataArray.length; i++) {dataArray[i] = data.charCodeAt(i);}
        }
      } else
      var dataArray = VBS_BinaryToArray(data).toArray(); //兼容IE低版本
      return this.getBase64(dataArray);
    },
    getBase64(dataArray) {
      var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
      var strData = "";
      for (var i = 0, ii = dataArray.length; i < ii; i += 3) {
        if (isNaN(dataArray[i])) break;
        var b1 = dataArray[i] & 0xFF, b2 = dataArray[i + 1] & 0xFF, b3 = dataArray[i + 2] & 0xFF;
        var d1 = b1 >> 2, d2 = ((b1 & 3) << 4) | (b2 >> 4);
        var d3 = i + 1 < ii ? ((b2 & 0xF) << 2) | (b3 >> 6) : 64;
        var d4 = i + 2 < ii ? (b3 & 0x3F) : 64;
        strData += digits.substring(d1, d1 + 1) + digits.substring(d2, d2 + 1) + digits.substring(d3, d3 + 1) + digits.substring(d4, d4 + 1);
      }
      return strData;
    }
  }
}
</script>

<style scoped>
.top_but {
  margin-left: 20px;
}

.top_buts {
  margin-top: 10px;
  margin-bottom: 10px;
}

.pagination {
  text-align: center;
}

.top_b {
  display: flex;
}

.top_inp {
  /* width: 300px; */
  display: flex;
}

.dialog {
  width: 500px;
  margin: 0px auto;
}

/*  */

.el-select {
  width: 300px;
}

.el-select /deep/ .el-input__inner {
  width: 100%;
}

.input_c {
  width: 300px;
}

::v-deep .input_c .el-input__inner {
  width: 100%;
}

.date {
  display: flex;
  justify-content: space-between;
}

</style>
